package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.service.ArticleService;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Value("${article.pagesize}")
    private Integer size;

    @RequestMapping("/selectAll")
    public String selectAll(Model model, HttpServletRequest request){
        String page1 = request.getParameter("page");
        if(StringUtils.isEmpty(page1)||"null".equals(page1)){
            page1 = "0";
        }
        Page<Article> articles = articleService.selectList(Integer.parseInt(page1), size);
        model.addAttribute("articles", articles.getContent());
        model.addAttribute("total", articles.getTotalElements());
        model.addAttribute("pageTotal", articles.getTotalPages());
        model.addAttribute("page",page1);
        return "client/index";
    }

}
